<?php

namespace App\Http\Controllers\Api;

use App\Consts\GlobalNoteConst;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

class HomeController extends BaseController
{
    public $validate = [
        "collectList" => ["rules" => [
            "last_id" => "sometimes|required|string|max:24",
        ]],
        "noteList" => ["rules" => [
            "last_id" => "sometimes|required|string|max:24",
        ]],
        "packageList" => ["rules" => [
            "last_id" => "sometimes|required|string|max:24",
        ]],
    ];

    /**
     * @path /home/collect-list
     * @category 桌面
     * @title 【有备注】收藏列表
     * @method POST
     * @params [{"name":"last_id","type":"text","required":0,"example":"","desc":"【翻页时必传】当前页最后一条记录的ID"}]
     * @resBody {"success":true,"statusCode":200,"message":"操作成功","result":{"have_more":true,"list":[{"id":"24位文章ID","cover_url":"文章封面图","title":"文章标题","description":"文章描述","is_deleted":0,"is_shield":0,"created_at":"10位时间戳","collect_id":"24位收藏ID，此字段作为翻页依据","package_id":"24位收藏夹ID","target_type":1},{"id":"24位文章笔记或划线笔记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"24位文章ID","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":"文章节选开始索引","end_index":"文章节选结束索引","content":"笔记内容","selected":"文章节选内容","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"article":{"id":"24位文章ID","cover_url":"文章封面图，如1.jpg","title":"文章标题","description":"文章描述","is_deleted":0,"is_shield":0,"created_at":"文章创建时间戳"},"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}},{"id":"24位文章笔记或划线笔记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"24位文章ID","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":"文章节选开始索引","end_index":"文章节选结束索引","content":"笔记内容","selected":"文章节选内容","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"image":["1.jpg"],"article":{},"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}}]}}
     * @description 1.【前端】翻页时，传的<last_id>是返回字段中的collect_id
     */
    public function collectList(Request $request)
    {
        $params = $request->all();

        $searchParams = [
            'user_id' => $this->getCurrentUserId(),
            'current_user_id' => $this->getCurrentUserId(),
        ];
        if (!empty($params['last_id'])) {
            $searchParams['last_id'] = $params['last_id'];
        }

        $collectList = service()->CollectService->getUserCollectList($searchParams);

        return $this->apiReturn($collectList);
    }

    /**
     * @path /home/note-list
     * @category 桌面
     * @title 【有备注】笔记列表
     * @method POST
     * @params [{"name":"last_id","type":"text","required":0,"example":"","desc":"【翻页时必传】当前页最后一条记录的ID"}]
     * @resBody {"success":true,"statusCode":200,"message":"操作成功","result":{"have_more":false,"list":[{"id":"24位文章笔记或划线笔记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"24位文章ID","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":"文章节选开始索引","end_index":"文章节选结束索引","content":"笔记内容","selected":"文章节选内容","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"article":{"id":"24位文章ID","cover_url":"文章封面图，如1.jpg","title":"文章标题","description":"文章描述","is_deleted":0,"is_shield":0,"created_at":"文章创建时间戳"},"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}},{"id":"24位随手记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":0,"end_index":0,"content":"笔记内容","selected":"","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"article":{},"image":["1.jpg","2.jpg"],"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}}]}}
     * @description 1.实例返回中包含了【文章笔记、划线笔记】和【随手记】两种节点
     */
    public function noteList(Request $request)
    {
        $params = $request->all();

        $data = service()->NoteService->userNoteList($this->getBaseInfo($params, [
            'tab' => GlobalNoteConst::TAB_TYPE_MY,
        ]));

        return $this->apiReturn($data);
    }

    /**
     * @path /home/package-list
     * @category 桌面
     * @title 收藏夹列表
     * @method POST
     * @params [{"name":"last_id","type":"text","required":0,"example":"","desc":"【翻页时必传】当前页最后一条记录的ID"}]
     * @resBody {"success":true,"statusCode":200,"message":"操作成功","result":{"have_more":true,"list":[{"id":"收藏夹ID","user_id":"用户ID","name":"收藏夹名称","description":"收藏夹描述","total":"收藏夹内容总数","is_default":1,"is_deleted":0,"is_public":1,"updated_at":"收藏夹最后更新时间"}]}}
     */
    public function packageList(Request $request)
    {
        $params = $request->all();

        $searchParams = [
            'user_id' => $this->getCurrentUserId(),
            'current_user_id' => $this->getCurrentUserId(),
        ];

        // 由于getCurrentUserId()会返回游客ID，为了防止游客创建默认收藏夹，需要将当前用户ID置空
        if ($this->isTourist) {
            $searchParams['current_user_id'] = '';
        }

        if (array_key_exists('last_id', $params)) {
            $searchParams['last_id'] = $params['last_id'];
        }

        $collectPackageList = service()->PackageService->getUserCollectPackageList($searchParams);

        return $this->apiReturn($collectPackageList);
    }
}
